package cn.jiyun.controller;

import cn.hutool.jwt.JWTUtil;
import cn.jiyun.common.model.day1.Person;
import cn.jiyun.common.vo.Result;
import cn.jiyun.service.UserService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

/**
 * @Author 李星月
 * @Date: 2023/8/22 15:33
 * @Description : UserController
 */
@RestController
@RequestMapping("/person")
public class UserController {
    @Autowired
    UserService userService;
    @Autowired
    RedisTemplate redisTemplate;

    @PostMapping("/userlogin")
    public Result userlogin(@RequestBody Person person){
        Person person1 = userService.userlogin(person);
        if (person1!=null){
            HashMap<String, Object> map = new HashMap<>();
            map.put("token",person1);
            String token = JWTUtil.createToken(map, "123".getBytes());
            redisTemplate.opsForValue().set(token, JSON.toJSONString(person1));
            return Result.success(token);
        }else {
            return Result.error();
        }
    }
}
